package cn.e4j.modular.api.controller;


import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.service.Gwat810Service;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.crm.entity.GwcwT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单运输评价 控制类
 * Created by shixc on 2018/5/8.
 */
@RestController
@RequestMapping("api/wx/gwat810")
public class Gwat810Controller {
    private static Logger logger = LoggerFactory.getLogger(Gwat810Controller.class);

    @Autowired
    Gwat810Service gwat810Service;

//    @Autowired
//    private Gwaa_tDao gwaa_tDao;

    @Autowired
    Gwat200Service gwat200Service;

    /**
     * 获取业务人员 订单运输评价
     *
     * @return
     */
    @RequestMapping(value = "getGwcuDatas")
    public ResponseData getGwcsDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        int errcode = 200;
        String errmsg = "ok";
        List<Dto> list = new ArrayList<Dto>();
        SysUser user = wapper.getSysUser();
        Dto gwaaDtoIn = Dtos.newDto();
        try {
            QueryWrapper<GwaaT> gwaaTQueryWrapper = new QueryWrapper<>();
            gwaaTQueryWrapper.eq("GWAAENT", inDto.getString("ENT")).eq("GWAA001", user.getACCOUNT());
            GwaaT gwaaT = new GwaaT().selectOne(gwaaTQueryWrapper);
            inDto.put("GWAA001", user.getACCOUNT());
            // 0.内部员工、1.外部人员
            if (gwaaT.getGWAA007().equals("0")) {
                //获取 分配的区域 --内部员工
                gwaaDtoIn.put("GWAA003", user.getACCOUNT());
                inDto.put("MMRE004S",
                        gwat200Service.getMmreData(inDto));

                if (inDto.getString("status").equalsIgnoreCase("N")) {  //status:N未评价Y:已评价
                    list = gwat810Service.getGwcu1(wapper.getPage(),inDto);
                } else if (inDto.getString("status").equalsIgnoreCase("Y")) {
                    list = gwat810Service.getGwcu2(wapper.getPage(),inDto);
                } else {
                    list = gwat810Service.getGwcu3(wapper.getPage(),inDto);
                }
            } else if (gwaaT.getGWAA007().equals("1")) {
                if (inDto.getString("status").equalsIgnoreCase("N")) {
                    list = gwat810Service.getGwcu4(wapper.getPage(),inDto);
                } else if (inDto.getString("status").equalsIgnoreCase("Y")) {
                    list = gwat810Service.getGwcu5(wapper.getPage(),inDto);
                } else {
                    list = gwat810Service.getGwcu6(wapper.getPage(),inDto);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            errcode = 500;
            errmsg = "服务器出小差了";
            return new ErrorResponseData(errcode,errmsg);
        }
        return new SuccessResponseData(list);
    }

    /**
     * 保存订单评价信息
     *
     * @return
     */
    @RequestMapping(value = "saveData")
    public ResponseData saveData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = new Dto();
        SysUser user = wapper.getSysUser();
        GwcwT gwcw_tPO = new GwcwT();
        List<GwcwT> gwcw_tPOs = new ArrayList<>();
        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            String GWCW004 = inJson.getString("GWCW004");
            String[] GWCW004s = GWCW004.split(",");
            String GWCW005 = inJson.getString("GWCW005");
            String[] GWCW005s = GWCW005.split(",");
            gwcw_tPO = CommonUtils.toJavaClass(inDto, GwcwT.class);
            gwcw_tPO.setGWCWENT(inJson.getString("GWCUENT"));
            gwcw_tPO.setGWCWDOCNO("SPPJ" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
            LocalDate date = LocalDate.now();
            gwcw_tPO.setGWCW001("2");
            gwcw_tPO.setGWCW002(inJson.getString("GWCUDOCNO"));
            gwcw_tPO.setGWCW003(inJson.getBigDecimal("GWCUSEQ"));
            gwcw_tPO.setGWCW006(inJson.getString("GWCW006"));
            gwcw_tPO.setGWCW007(inJson.getString("GWCW007"));
            gwcw_tPO.setGWCW008(inJson.getString("GWCW008"));
            QueryWrapper<GwaaT> gwaaTQueryWrapper = new QueryWrapper<>();
            gwaaTQueryWrapper.eq("GWAAENT", inDto.getString("ENT")).eq("GWAA001", user.getACCOUNT());
            GwaaT gwaaT = new GwaaT().selectOne(gwaaTQueryWrapper);
            gwcw_tPO.setGWCW009(gwaaT.getGWAA002());
            gwcw_tPO.setGWCW010(gwaaT.getGWAA004());
            gwcw_tPO.setGWCW011(gwaaT.getGWAA007());
            gwcw_tPO.setGWCW012(gwaaT.getGWAA003());
            gwcw_tPO.setGWCW020(inJson.getString("GWCS001"));
            gwcw_tPO.setGWCWORIU(user.getID());
            for (int i = 0; i < GWCW004s.length; i++) {
                GwcwT gwcw_tPO_ = CommonUtils.toJavaClass(gwcw_tPO,GwcwT.class);
                gwcw_tPO_.setGWCWDOCDATE(date);
                gwcw_tPO_.setGWCWSEQ(BigDecimal.valueOf(i + 1));
                gwcw_tPO_.setGWCW004(GWCW004s[i]);
                gwcw_tPO_.setGWCW005(GWCW005s[i]);
                gwcw_tPOs.add(gwcw_tPO_);
            }
            gwat810Service.saveData(gwcw_tPOs);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(500,"服务器出小差了");
        }
        return new SuccessResponseData();
    }

    /**
     * 获取业务人员 订单运输评价
     *
     * @return
     */
    @RequestMapping(value = "getMmrj")
    public ResponseData getMmrj(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto outDto = new Dto();
        List<Dto> list = new ArrayList<Dto>();
        try {
            inDto.put("MMRJENT", inDto.getString("ENT"));
            list = gwat810Service.getMmrj(inDto);
            outDto.put("data", list);
            outDto.setAppCode(200);
            outDto.setAppMsg("OK");
        } catch (Exception e) {
            e.printStackTrace();
            outDto.setAppCode(500);
            outDto.setAppMsg("服务器出小差了");
        }
        return new SuccessResponseData(outDto);
    }
}
